{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1. run_until_complete\n",
    "# import asyncio\n",
    "# loop = asyncio.get_event_loop()\n",
    "# loop.run_forever()\n",
    "# loop.run_until_complete()\n",
    "\n",
    "#1. loop会被放到future中\n",
    "#2. 取消future(task)\n",
    "\n",
    "# import asyncio\n",
    "# import time\n",
    "#\n",
    "# async def get_html(sleep_times):\n",
    "#     print(\"waiting\")\n",
    "#     await asyncio.sleep(sleep_times)\n",
    "#     print(\"done after {}s\".format(sleep_times))\n",
    "#\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     task1 = get_html(2)\n",
    "#     task2 = get_html(3)\n",
    "#     task3 = get_html(3)\n",
    "#\n",
    "#     tasks = [task1, task2, task3]\n",
    "#\n",
    "#     loop = asyncio.get_event_loop()\n",
    "#\n",
    "#     try:\n",
    "#         loop.run_until_complete(asyncio.wait(tasks))\n",
    "#     except KeyboardInterrupt as e:\n",
    "#         all_tasks = asyncio.Task.all_tasks()\n",
    "#         for task in all_tasks:\n",
    "#             print(\"cancel task\")\n",
    "#             print(task.cancel())\n",
    "#         loop.stop()\n",
    "#         loop.run_forever()\n",
    "#     finally:\n",
    "#         loop.close()\n",
    "\n",
    "#注册协程到loop中有两种方法\n",
    "#1. 通过ensure_future/create_task\n",
    "#2. await"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 子协程时序\n",
    "https://docs.python.org/3/library/asyncio-task.html"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
